package com.freesky.provider.order.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freesky.common.enums.StateEnum;
import com.freesky.common.exception.EMallException;
import com.freesky.common.pojo.ZTreeNode;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.common.dao.entity.ItemCat;
import com.freesky.provider.order.service.IItemCatService;
import com.freesky.provider.order.util.VOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Freesky
 * @date 2017/8/2
 */
@Service
public class ItemCatServiceImpl implements IItemCatService {
    private Logger logger = LoggerFactory.getLogger(ItemCatServiceImpl.class);

    @Override
    public ItemCat getItemCatById(Long id) {

        ItemCat itemCat = new ItemCat(id);
        itemCat = itemCat.selectById();
        if (itemCat == null) {
            throw new EMallException("通过id获取商品分类失败");
        }
        return itemCat;
    }

    @Override
    public List<ZTreeNode> getItemCatList(int parentId) {

        List<ItemCat> list = new ItemCat().selectList(new EntityWrapper().eq("state",StateEnum.INUSE)
                .eq("parent_id", parentId).orderBy("sort_order", true));

        //转换成ZtreeNode
        List<ZTreeNode> resultList = new ArrayList<>();
        for (ItemCat itemCat : list) {
            ZTreeNode node = VOUtil.changeItemCatToZTreeNode(itemCat);
            resultList.add(node);
        }
        return resultList;
    }

    @Override
    public JSONResponse addItemCat(ItemCat itemCat) {

        if (itemCat.getParentId() == 0) {
            //根节点
            itemCat.setSortOrder(0);
            itemCat.setState(StateEnum.INUSE);
        } else {
            itemCat.setSortOrder(0);
            itemCat.setState(StateEnum.INUSE);
            itemCat.setCreated(new Date());
            itemCat.setUpdated(new Date());
        }
        try {
            itemCat.insert();
        } catch (Exception e) {
            logger.error("添加商品分类失败", e);
            throw new EMallException("添加商品分类失败");
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse updateItemCat(ItemCat itemCat) {
        ItemCat old = getItemCatById(itemCat.getId());
        itemCat.setCreated(old.getCreated());
        itemCat.setUpdated(new Date());
        boolean update = itemCat.updateById();
        if (!update) {
            throw new EMallException("添加商品分类失败");
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse deleteItemCat(Long id) {
        deleteZTree(id);
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public void deleteZTree(Long id) {

        //查询该节点所有子节点
        List<ZTreeNode> node = getItemCatList(Math.toIntExact(id));
        if (node.size() > 0) {
            //如果有子节点，遍历子节点
            for (int i = 0; i < node.size(); i++) {
                deleteItemCat((long) node.get(i).getId());
            }
        }
        ItemCat itemCat = new ItemCat(id);
        boolean delete = itemCat.deleteById();
        //没有子节点直接删除
        if (!delete) {
            throw new EMallException("删除商品分类失败");
        }
    }

}
